package org.jeecg.talentCooperation.common;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecg.talentCooperation.common.vo.Result;
import org.jeecg.talentCooperation.fruit.entity.Fruit;
import org.jeecg.talentCooperation.fruit.entity.FruitAuthor;
import org.jeecg.talentCooperation.fruit.entity.FruitAuthorOrg;
import org.jeecg.talentCooperation.fruit.entity.FruitFieldRel;
import org.jeecg.talentCooperation.fruit.service.IFruitFieldRelService;
import org.jeecg.talentCooperation.fruit.service.IFruitService;
import org.jeecg.talentCooperation.sysdict.po.NdSysDict;
import org.jeecg.talentCooperation.sysdict.service.INdSysDictService;
import org.jeecg.talentCooperation.talent.entity.TalentBasic;
import org.jeecg.talentCooperation.talent.entity.TalentBasicBankInfo;
import org.jeecg.talentCooperation.talent.entity.TalentBasicDealInfo;
import org.jeecg.talentCooperation.talent.service.ITalentBasicBankInfoService;
import org.jeecg.talentCooperation.talent.service.ITalentBasicDealInfoService;
import org.jeecg.talentCooperation.talent.service.ITalentBasicService;
import org.jeecg.talentCooperation.thread.AsyncService;
import org.jeecg.talentCooperation.util.PinyinUtil;
import org.jeecg.talentCooperation.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/ndTalent/talent/common")
public class TalentCommonController {

    @Autowired
    private ITalentBasicService basicService;

    @Autowired
    private ITalentBasicBankInfoService basicBankInfoService;

    @Autowired
    private ITalentBasicDealInfoService basicDealInfoService;

    @Autowired
    private IFruitService fruitService;

    @Autowired
    private IFruitFieldRelService fruitFieldService;

    @Autowired
    private INdSysDictService sysDictService;

    @Autowired
    AsyncService asyncService;

    /**
     * 专家数据导入
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/importExpertDataExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            MultipartFile multipartFile = entity.getValue();// 获取上传文件对象
            // 获取文件原始名称
            String originalFileName = multipartFile.getOriginalFilename();
            log.info("------------正在导入作者--------" + originalFileName);
            // 创建临时文件
            assert originalFileName != null;
            File file = File.createTempFile("temp", originalFileName.substring(originalFileName.lastIndexOf(".")));
            // 将上传文件转为临时文件
            multipartFile.transferTo(file);
            // 退出时删除临时文件
            file.deleteOnExit();

            Workbook workbook = null;
            try {
                if (file.getPath().endsWith("xls")) {
                    workbook = new HSSFWorkbook(new FileInputStream(file));
                } else if (file.getPath().endsWith("xlsx")) {
                    workbook = new XSSFWorkbook(new FileInputStream(file));
                }
                // 获取第一个sheet
                Sheet sheet = workbook.getSheetAt(0);
                Map<String, TalentBasic> map = new HashMap<>();
                List<TalentBasicBankInfo> allBankInfoList = new ArrayList<>();
                List<TalentBasicDealInfo> allDealInfoList = new ArrayList<>();
                // 获取每行中的字段,过滤首行说明和第二行字段信息
                for (int i = 2; i <= sheet.getLastRowNum(); i++) {
                    log.info("-----共有---"+(sheet.getLastRowNum() -1)+"---行----正在导入第--------" + (i-1) + "---行数据");
                    Row row = sheet.getRow(i);
                    String dealNumber = row.getCell(1).getStringCellValue().trim();
                    String dealName = row.getCell(2).getStringCellValue().trim();
                    String dealDeptNumber = row.getCell(3).getStringCellValue().trim();
                    String dealDeptName = row.getCell(4).getStringCellValue().trim();
                    String supplyNumber = row.getCell(5).getStringCellValue().trim();
                    String talentNumber = row.getCell(6).getStringCellValue().trim();
                    String talentName = row.getCell(7).getStringCellValue().trim();
                    String companyCode = row.getCell(8).getStringCellValue().trim();
                    String accountName = row.getCell(9).getStringCellValue().trim();
                    String accountBank = row.getCell(10).getStringCellValue().trim();
                    String accountBankName = row.getCell(11).getStringCellValue().trim();
                    String accountBankNumber = row.getCell(12).getStringCellValue().trim();
                    String bankCode = row.getCell(13).getStringCellValue().trim();
                    String headBankCode = row.getCell(14).getStringCellValue().trim();
                    String idCard = row.getCell(15).getStringCellValue().trim();
                    String phoneNumber = row.getCell(16).getStringCellValue().trim();
                    String company = row.getCell(17).getStringCellValue().trim();
                    String researchDomain = row.getCell(18).getStringCellValue().trim();
                    String theTitle = row.getCell(19).getStringCellValue().trim();
                    String facultySources = row.getCell(20).getStringCellValue().trim();
                    String photoNumber = row.getCell(21).getStringCellValue().trim();
                    String titleLevel = row.getCell(22).getStringCellValue().trim();
                    String post = row.getCell(23).getStringCellValue().trim();
                    String accessed = row.getCell(24).getStringCellValue().trim();
                    String cooperationNum = "";

                    if (CellType.NUMERIC == row.getCell(25).getCellTypeEnum()) {
                        cooperationNum = row.getCell(25).getNumericCellValue() + "";
                    } else {
                        cooperationNum =  row.getCell(25).getStringCellValue().trim();
                    }


                    //用talentNumber共能导入9697条数据，但其中会有姓名（单位不同）重复，用名称作为key，能导入8972条数据
                    String mapKey = talentNumber;

                    TalentBasic talentBasic = new TalentBasic();
                    List<TalentBasicDealInfo> dealInfoList = new ArrayList<>();
                    List<TalentBasicBankInfo> bankInfoList = new ArrayList<>();

                    if(map.get(mapKey) != null){
                        talentBasic = map.get(mapKey);

                        if(!StringUtils.isEmpty(talentBasic.getAllPhoneNumber()) && !phoneNumber.equals(talentBasic.getAllPhoneNumber())){
                            if(!StringUtils.isEmpty(phoneNumber) && ToolUtil.isNormalPhoneNumber(phoneNumber)){
                                phoneNumber += "," + talentBasic.getAllPhoneNumber();
                            }else{
                                phoneNumber = talentBasic.getAllPhoneNumber();
                            }
                        }
                        company = setValue(talentBasic.getCompany(), company);
                        researchDomain = setValue(talentBasic.getResearchDomain(), researchDomain);
                        theTitle = setValue(talentBasic.getTheTitle(), theTitle);
                        facultySources = setValue(talentBasic.getFacultySources(), facultySources);
                        titleLevel = setValue(talentBasic.getTitleLevel(), titleLevel);
                        post = setValue(talentBasic.getPost(), post);

                        //不一致就新增
                        if(talentBasic.getBankInfoList().stream().noneMatch(talentBasicBankInfo -> talentBasicBankInfo.getAccountBankNumber().equals(accountBankNumber))){
                            TalentBasicBankInfo talentBasicBankInfo = new TalentBasicBankInfo(talentNumber, accountName, accountBank, accountBankName, accountBankNumber, bankCode, headBankCode);
                            talentBasic.getBankInfoList().add(talentBasicBankInfo);
                        }

                        TalentBasicDealInfo talentBasicDealInfo = new TalentBasicDealInfo(talentNumber, dealNumber, dealName, dealDeptNumber, dealDeptName, supplyNumber, companyCode);
                        talentBasic.getDealInfoList().add(talentBasicDealInfo);
                    }else{
                        if(!ToolUtil.isNormalPhoneNumber(phoneNumber)){
                            phoneNumber = "";
                        }
                        TalentBasicDealInfo talentBasicDealInfo = new TalentBasicDealInfo(talentNumber, dealNumber, dealName, dealDeptNumber, dealDeptName, supplyNumber, companyCode);
                        dealInfoList.add(talentBasicDealInfo);
                        talentBasic.setDealInfoList(dealInfoList);

                        TalentBasicBankInfo talentBasicBankInfo = new TalentBasicBankInfo(talentNumber, accountName, accountBank, accountBankName, accountBankNumber, bankCode, headBankCode);
                        bankInfoList.add(talentBasicBankInfo);
                        talentBasic.setBankInfoList(bankInfoList);
                    }
                    talentBasic.setTalentNumber(talentNumber);
                    talentBasic.setRealName(talentName);
                    talentBasic.setNamePinyin(PinyinUtil.getPinYinPrefix(talentName, true));
                    talentBasic.setAllPhoneNumber(phoneNumber);
                    talentBasic.setPartPhoneNumber(ToolUtil.maskPhoneNumber(phoneNumber));
                    talentBasic.setCompany(company);
                    talentBasic.setResearchDomain(researchDomain);
                    talentBasic.setTheTitle(theTitle);
                    talentBasic.setFacultySources(facultySources);
                    talentBasic.setTitleLevel(titleLevel);
                    talentBasic.setPost(post);
                    talentBasic.setIdNum(idCard);
                    talentBasic.setPhotoNumber(photoNumber);
                    talentBasic.setAccessed(accessed);
                    talentBasic.setCooperationNum(cooperationNum);

                    map.put(mapKey, talentBasic);
                    allBankInfoList.addAll(talentBasic.getBankInfoList());
                    allDealInfoList.addAll(talentBasic.getDealInfoList());

                    // 根据 ruleCode 查询出实体
//                    QueryWrapper queryWrapper = new QueryWrapper();
//                    queryWrapper.eq("talent_number", talentNumber);
//                    TalentBasic talentBasic =  basicService.getOne(queryWrapper);
//                    if(talentBasic != null){
//                        if(!StringUtils.isEmpty(talentBasic.getPhoneNumber()) && !phoneNumber.equals(talentBasic.getPhoneNumber())
////                                && !("12345678912").equals(talentBasic.getPhoneNumber())
////                                && talentBasic.getPhoneNumber().length() != 1 && talentBasic.getPhoneNumber().length() != 4
//                        ){
//                            if(!StringUtils.isEmpty(phoneNumber)){
//                                phoneNumber += "," + talentBasic.getPhoneNumber();
//                            }else{
//                                phoneNumber = talentBasic.getPhoneNumber();
//                            }
//                        }
//                        company = setValue(talentBasic.getCompany(), company);
//                        researchDomain = setValue(talentBasic.getResearchDomain(), researchDomain);
//                        theTitle = setValue(talentBasic.getTheTitle(), theTitle);
//                        facultySources = setValue(talentBasic.getFacultySources(), facultySources);
//                        titleLevel = setValue(talentBasic.getTitleLevel(), titleLevel);
//                        post = setValue(talentBasic.getPost(), post);
//                    }else{
//                        talentBasic = new TalentBasic();
//                        talentBasic.setTalentNumber(talentNumber);
//                    }
                }
                int batchSize = 1000; // 每次处理的批次大小

                if(!CollectionUtils.isEmpty(map)){
                    List<TalentBasic> talentBasicList = new ArrayList<>(map.values());
                    for (int i = 0; i < talentBasicList.size(); i += batchSize) {
                        List<TalentBasic> batch = talentBasicList.subList(i, Math.min(i + batchSize, talentBasicList.size()));
                        basicService.saveOrUpdateBatch(batch);
                    }
                }

                if(!CollectionUtils.isEmpty(allBankInfoList)){
                    for (int i = 0; i < allBankInfoList.size(); i += batchSize) {
                        List<TalentBasicBankInfo> batch = allBankInfoList.subList(i, Math.min(i + batchSize, allBankInfoList.size()));
                        basicBankInfoService.saveOrUpdateBatch(batch);
                    }
                }

                if(!CollectionUtils.isEmpty(allDealInfoList)){
                    for (int i = 0; i < allDealInfoList.size(); i += batchSize) {
                        List<TalentBasicDealInfo> batch = allDealInfoList.subList(i, Math.min(i + batchSize, allDealInfoList.size()));
                        basicDealInfoService.saveOrUpdateBatch(batch);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                return Result.error("格式异常，请修改后再试！");
            } finally{
                workbook.close();
            }
            log.info("------------导入结束-----------");
        }
        return Result.ok("导入专家数据成功!");

    }


    /**
     * 院士专家数据导入
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/importYsExpertDataExcel", method = RequestMethod.POST)
    public Result<?> importYsExcel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
        for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
            MultipartFile multipartFile = entity.getValue();// 获取上传文件对象
            // 获取文件原始名称
            String originalFileName = multipartFile.getOriginalFilename();
            log.info("------------正在导入作者--------" + originalFileName);
            // 创建临时文件
            assert originalFileName != null;
            File file = File.createTempFile("temp", originalFileName.substring(originalFileName.lastIndexOf(".")));
            // 将上传文件转为临时文件
            multipartFile.transferTo(file);
            // 退出时删除临时文件
            file.deleteOnExit();

            Workbook workbook = null;
            try {
                if (file.getPath().endsWith("xls")) {
                    workbook = new HSSFWorkbook(new FileInputStream(file));
                } else if (file.getPath().endsWith("xlsx")) {
                    workbook = new XSSFWorkbook(new FileInputStream(file));
                }
                // 获取第一个sheet
                Sheet sheet = workbook.getSheetAt(0);
                Map<String, TalentBasic> map = new HashMap<>();
                // 获取每行中的字段,过滤首行说明和第二行字段信息
                for (int i = 2; i <= sheet.getLastRowNum(); i++) {
                    log.info("------------正在导入第--------" + (i-1) + "---行数据");
                    Row row = sheet.getRow(i);
                    String workingDept = row.getCell(2).getStringCellValue();
                    String talentName = row.getCell(3).getStringCellValue();
                    String company = row.getCell(4).getStringCellValue();
//                    String workingDept = row.getCell(4).getStringCellValue();
                    DataFormatter formatter = new DataFormatter();
                    String phoneNum = formatter.formatCellValue(row.getCell(5));
                    String email = row.getCell(6).getStringCellValue();
                    String summary = row.getCell(7).getStringCellValue();
                    String researchDomain = (!ObjectUtils.isEmpty(row.getCell(9))) ? row.getCell(9).getStringCellValue() : "";
                    String linkAddress = row.getCell(10).getStringCellValue();
                    String profilePhoto = row.getCell(11).getStringCellValue();

                    TalentBasic talentBasic = new TalentBasic();
                    if(map.get(talentName) != null){
                        talentBasic = map.get(talentName);
                    }
                    talentBasic.setRealName(talentName);
                    talentBasic.setNamePinyin(PinyinUtil.getPinYinPrefix(talentName, true));
                    talentBasic.setCompany(company);
                    talentBasic.setWorkingDept(workingDept);
                    talentBasic.setAllPhoneNumber(phoneNum);
                    talentBasic.setPartPhoneNumber(phoneNum);
                    talentBasic.setMail(email);
                    talentBasic.setResearchDomain(researchDomain);
                    talentBasic.setWorkingDept(workingDept);
                    talentBasic.setLinkAddress(linkAddress);
                    talentBasic.setSummary(summary);
                    talentBasic.setProfilePhoto(ToolUtil.imageToBase64(profilePhoto));
                    talentBasic.setType(request.getParameter("talentType"));

                    map.put(talentName, talentBasic);

//                    // 根据 ruleCode 查询出实体
//                    QueryWrapper queryWrapper = new QueryWrapper();
//                    queryWrapper.eq("talent_number", talentNumber);
//                    TalentBasic talentBasic =  basicService.getOne(queryWrapper);
//                    if(talentBasic != null){
//                        if(!StringUtils.isEmpty(talentBasic.getPhoneNumber()) && !phoneNumber.equals(talentBasic.getPhoneNumber())
////                                && !("12345678912").equals(talentBasic.getPhoneNumber())
////                                && talentBasic.getPhoneNumber().length() != 1 && talentBasic.getPhoneNumber().length() != 4
//                        ){
//                            if(!StringUtils.isEmpty(phoneNumber)){
//                                phoneNumber += "," + talentBasic.getPhoneNumber();
//                            }else{
//                                phoneNumber = talentBasic.getPhoneNumber();
//                            }
//                        }
//                        company = setValue(talentBasic.getCompany(), company);
//                        researchDomain = setValue(talentBasic.getResearchDomain(), researchDomain);
//                        theTitle = setValue(talentBasic.getTheTitle(), theTitle);
//                        facultySources = setValue(talentBasic.getFacultySources(), facultySources);
//                        titleLevel = setValue(talentBasic.getTitleLevel(), titleLevel);
//                        post = setValue(talentBasic.getPost(), post);
//                    }else{
//                        talentBasic = new TalentBasic();
//                        talentBasic.setTalentNumber(talentNumber);
//                    }
                }
                int batchSize = 1000; // 每次处理的批次大小

                if(!CollectionUtils.isEmpty(map)){
                    List<TalentBasic> talentBasicList = new ArrayList<>(map.values());
                    for (int i = 0; i < talentBasicList.size(); i += batchSize) {
                        List<TalentBasic> batch = talentBasicList.subList(i, Math.min(i + batchSize, talentBasicList.size()));
                        basicService.saveOrUpdateBatch(batch);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                return Result.error("格式异常，请修改后再试！");
            }
            log.info("------------导入结束-----------");
        }
        return Result.ok("导入专家数据成功!");

    }



    public String setValue(String oldVal, String newVal){
        if(!StringUtils.isEmpty(oldVal)){
            if(!StringUtils.isEmpty(newVal) && !oldVal.equals(newVal)){
                oldVal += "," + newVal;
            }
        }else{
            oldVal = newVal;
        }
        return oldVal;
    }

    /**
     * 导入成果
     */
//    @Transactional(isolation = Isolation.READ_COMMITTED)
    @RequestMapping(value = "/importCnkiFruit", method = RequestMethod.POST)
    public Result<?> importCnkiFruit(@RequestParam("files") MultipartFile[] files, HttpServletResponse response) throws IOException {
//        int index = 0;
//        //获取研究领域字典表
//        QueryWrapper queryWrapper = new QueryWrapper();
//        queryWrapper.eq("type_key", "ResearchField");
//        List<NdSysDict> ndSysDictList =  sysDictService.list(queryWrapper);
//        Map<String, String> sysDictMap = ndSysDictList.stream().collect(Collectors.toMap(NdSysDict::getDName, NdSysDict::getDValue));
//        List<NdSysDict> toUpdateNdSysDictList = new ArrayList<>();
//        //获取成果研究领域
////        List<FruitField> fruitFieldList =  fruitFieldService.list();
////        Map<Long, FruitField> fruitFieldMap = fruitFieldList.stream().collect(Collectors.toMap(FruitField::getFruitId, Function.identity()));
//        List<FruitFieldRel> toUpdateFruitFieldRelList = new ArrayList<>();
//        // 所有专家数据
//        List<TalentBasic> talentBasicList =  basicService.list();
//        Map<String, TalentBasic> talentBasicMap = talentBasicList.stream().collect(Collectors.toMap(TalentBasic::getRealName, Function.identity()));
//        List<TalentBasic> toUpdateBasicList = new ArrayList<>();
//        //成果数据
//        List<Fruit> fruitList =  fruitService.list();
//        Map<String, Fruit> fruitListMap = fruitList.stream().collect(Collectors.toMap(Fruit::getName, Function.identity()));
//        List<Fruit> toUpdateFruitList = new ArrayList<>();
//        //成果作者数据
//        List<FruitAuthor> fruitAuthorList = new ArrayList<>();
//        //成果作者机构数据
//        List<FruitAuthorOrg> fruitAuthorOrgList = new ArrayList<>();
//
//        List<JSONObject> allLines = new ArrayList<>();
//        for (MultipartFile multipartFile : files) {
//            index++;
//            // 获取文件原始名称
//            String originalFileName = multipartFile.getOriginalFilename();
//            System.out.println("---一共有---"+files.length+"---个作者------开始处理第"+index+"作者--------" + originalFileName + "-------数据---");
//
//            // 创建临时文件
//            assert originalFileName != null;
//            File file = File.createTempFile("temp", originalFileName.substring(originalFileName.lastIndexOf(".")));
//            // 将上传文件转为临时文件
//            multipartFile.transferTo(file);
//            // 退出时删除临时文件
//            file.deleteOnExit();
//            try {
//
//                try (BufferedReader br = new BufferedReader(new FileReader(file))) {
//                    String line;
//                    while ((line = br.readLine()) != null) {
////                        System.out.println(line);
////                        JSONObject jsonObject = JSON.parseObject(line);
//                        allLines.addAll(JSON.parseArray(line, JSONObject.class));
//                    }
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//
//            } catch (Exception e) {
//                e.printStackTrace();
//                return Result.error("格式异常，请修改后再试！");
//            }
//
//        }
//        //处理所有的行
//        try {
//            System.out.println("---一共有---" + allLines.size() + "条-------数据---");
//            if(!CollectionUtils.isEmpty(allLines)){
//                asyncService.dealFruit(allLines, sysDictMap, toUpdateNdSysDictList, toUpdateFruitFieldRelList,
//                        talentBasicMap, toUpdateBasicList, fruitListMap, toUpdateFruitList, fruitAuthorList, fruitAuthorOrgList);
//            }
//
//            System.out.println("-------入库操作------开始-----");
//
//            asyncService.dealData(toUpdateNdSysDictList, toUpdateFruitFieldRelList, toUpdateBasicList,
//                    toUpdateFruitList, fruitAuthorList, fruitAuthorOrgList);
//
//            System.out.println("-------入库操作------结束-----");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
        return Result.ok("导入成果数据成功!");

    }

    @RequestMapping(value = "/importCnkiFruitOfFolder", method = RequestMethod.GET)
    public Result<?> importCnkiFruitOfFolder(@RequestParam("folderPath") String folderPath, @RequestParam("importType") String importType, @RequestParam("expand") boolean expand) throws IOException {

        File directory = new File(folderPath);

        Path startPath = Paths.get(folderPath);
        // 打印所有文件的绝对路径
        List<File> fileList = new ArrayList<>();
        Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                fileList.add(file.toFile());
                return FileVisitResult.CONTINUE;
            }
        });
        System.out.println("---------共获取到目录及子目录下---------" + fileList.size() +"--个文件");
        if(!CollectionUtils.isEmpty(fileList)){
            try {
                this.toDealFruit(fileList, importType, expand);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return Result.ok("导入成果数据成功!");

    }

    public void toDealFruit(List<File> files, String importType, boolean expand) throws Exception {
        //获取研究领域字典表
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("type_key", "ResearchField");
        List<NdSysDict> ndSysDictList =  sysDictService.list(queryWrapper);
        Map<String, String> sysDictMap = ndSysDictList.stream().collect(Collectors.toMap(NdSysDict::getDName, NdSysDict::getDValue));
        List<NdSysDict> toUpdateNdSysDictList = new ArrayList<>();
        //获取成果研究领域
//        List<FruitField> fruitFieldList =  fruitFieldService.list();
//        Map<Long, FruitField> fruitFieldMap = fruitFieldList.stream().collect(Collectors.toMap(FruitField::getFruitId, Function.identity()));
        List<FruitFieldRel> toUpdateFruitFieldRelList = new ArrayList<>();
        // 所有专家数据
        List<TalentBasic> talentBasicList =  basicService.list();
        Map<String, TalentBasic> talentBasicMap = talentBasicList.stream().collect(Collectors.toMap(talentBasic -> talentBasic.getRealName()+"##"+talentBasic.getCompany(), Function.identity()));
        List<TalentBasic> toUpdateBasicList = new ArrayList<>();
        //成果数据
        List<Fruit> fruitList =  fruitService.list();
        Map<String, Fruit> fruitListMap = fruitList.stream().collect(Collectors.toMap(Fruit::getName, Function.identity()));
        List<Fruit> toUpdateFruitList = new ArrayList<>();
        //成果作者数据
        List<FruitAuthor> fruitAuthorList = new ArrayList<>();
        //成果作者机构数据
        List<FruitAuthorOrg> fruitAuthorOrgList = new ArrayList<>();

        int index = 0;
        List<JSONObject> allLines = new ArrayList<>();
        for (File file : files) {
            if(!(".DS_Store").equals(file.getName())){
                index ++;
                System.out.println("---------共获取到目录下----" + files.size() +"--个文件,开始读取第--"+ index+ "--个文件,共获取到----" + allLines.size() +"--行--");
                try (BufferedReader br = new BufferedReader(new FileReader(file))) {
                    String line;
                    while ((line = br.readLine()) != null) {
                        if(("1").equals(importType)){
                            System.out.println("---------获取到行数据----" + line +"----");
                            allLines.add(JSON.parseObject(line, Feature.OrderedField));
                        }else{
                            allLines.addAll(JSON.parseArray(line, JSONObject.class));
                            System.out.println("---------共获取到----" + JSON.parseArray(line, JSONObject.class).size() +"--行--");
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        //处理所有的行
        System.out.println("---文件夹一共有---" + allLines.size() + "条-------数据---");
        if(!CollectionUtils.isEmpty(allLines)){
            asyncService.dealFruit(allLines, sysDictMap, expand, toUpdateNdSysDictList, toUpdateFruitFieldRelList,
                    talentBasicList, talentBasicMap, toUpdateBasicList, fruitListMap, toUpdateFruitList, fruitAuthorList, fruitAuthorOrgList);
        }
        System.out.println("-------入库操作------开始-----");

        asyncService.dealData(toUpdateNdSysDictList, toUpdateFruitFieldRelList, toUpdateBasicList,
                toUpdateFruitList, fruitAuthorList, fruitAuthorOrgList);

        System.out.println("-------入库操作------结束-----");
    }


    /**
     * 处理专家相似度合并
     */
    @RequestMapping(value = "/dealExpertSimilarity", method = RequestMethod.POST)
    public Result<?> dealExpertSimilarity(){
        List<TalentBasic> talentBasicList = basicService.list();
        System.out.println("-------处理专家相似度合并-----");
        if(!CollectionUtils.isEmpty(talentBasicList)){
            try{
                asyncService.dealExpertSimilarity(talentBasicList);
            } catch (Exception e) {
                e.printStackTrace();
                return Result.error("处理专家相似度合并！");
            }
        }
        System.out.println("-------处理专家相似度合并--结束---");
        return Result.ok("处理数据成功!");
    }

    /**
     * 处理成果关键词和国网关键词的匹配
     */
    @RequestMapping(value = "/dealFruitKeyword", method = RequestMethod.POST)
    public Result<?> dealFruitKeyword(){
//        QueryWrapper queryWrapper = new QueryWrapper();
//        List<String> idList = new ArrayList<>();
//        idList.add("475940135574311032");
//        idList.add("475940140280320011");
//        queryWrapper.in("id", idList);
//        List<Fruit> fruitList =  fruitService.list(queryWrapper);
        List<Fruit> fruitList =  fruitService.list();

        System.out.println("-------开始处理成果关键词-----");
        if(!CollectionUtils.isEmpty(fruitList)){
            try{
                asyncService.getFruitKeyWordsAndSave(fruitList);
            } catch (Exception e) {
                e.printStackTrace();
                return Result.error("处理成果数据失败！");
            }
        }
        System.out.println("-------处理成果关键词--结束---");
        return Result.ok("处理成果数据成功!");
    }


    /**
     * 处理专家领域关键词
     */
    @RequestMapping(value = "/dealTalentField", method = RequestMethod.POST)
    public Result<?> dealTalentField(){
//        System.out.println("---kddd--");
//        List<TalentBasic> list = basicService.list();
//        list.forEach(talentBasic -> {
//            talentBasic.setNamePinyin(PinyinUtil.getPinYinPrefix(talentBasic.getRealName(), true));
//        });
//        int batchSize = 2000;
//        for (int i = 0; i < list.size(); i += batchSize) {
//            System.out.println("---111111--");
//            int end = Math.min(i + batchSize, list.size());
//            List<TalentBasic> batch = list.subList(i, end);
//            basicService.saveOrUpdateBatch(batch);
//        }
//        System.out.println("---end--");
        try{
            System.out.println("-------处理专家领域关键词-----");
            asyncService.dealTalentFields();
            System.out.println("-------处理专家领域关键词-结束---");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("处理专家领域关键词失败！");
        }
        return Result.ok("处理专家领域关键词成功!");
    }

    /**
     * 处理成果领域新分类
     */
//    @RequestMapping(value = "/dealFruitNewField", method = RequestMethod.POST)
//    public Result<?> dealFruitNewField(){
//        try{
//            asyncService.dealFruitNewField();
//        } catch (Exception e) {
//
//            e.printStackTrace();
//            return Result.error("处理专家领域失败！");
//        }
//        return Result.ok("处理专家领域成功!");
//    }

    /**
     * 处理国网专有（研究领域方向关键词excel）
     */
    @RequestMapping(value = "/importFkExcel", method = RequestMethod.POST)
    public Result<?> importFkExcel(HttpServletRequest request){
        try {
            asyncService.dealGwFieldKeyWord(request);
        } catch (IOException e) {
            e.printStackTrace();
            return Result.error("导入专家数据失败!");
        }
        return Result.ok("导入专家数据成功!");
    }

//    /**
//     * 处理成果关键词和国网关键词的匹配
//     */
//    @RequestMapping(value = "/dealFruitKwOfGwKw", method = RequestMethod.POST)
//    public Result<?> dealFruitKwOfGwKw() {
//        try {
//            asyncService.dealFruitKwOfGwKw();
//        } catch (Exception e) {
//            e.printStackTrace();
//            return Result.error("处理数据失败!");
//        }
//        return Result.ok("处理数据成功!");
//    }

}
